package com.dykj.fireFighting.server.socialUnit.firemanger.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.dykj.fireFighting.api.socialUnit.devicemanger.entity.PartitionsDevice;
import com.dykj.fireFighting.api.socialUnit.devicemanger.service.IPartitionsDeviceService;
import com.dykj.fireFighting.api.socialUnit.firemanger.entity.CoordinateRelation;
import com.dykj.fireFighting.api.socialUnit.firemanger.entity.KeyParts;
import com.dykj.fireFighting.api.socialUnit.firemanger.entity.KeyPartsDevice;
import com.dykj.fireFighting.api.socialUnit.firemanger.service.ICoordinateRelationService;
import com.dykj.fireFighting.api.socialUnit.firemanger.service.IKeyPartsDeviceService;
import com.dykj.fireFighting.api.socialUnit.firemanger.service.IKeyPartsService;
import com.dykj.fireFighting.api.socialUnit.firemanger.vo.KeyPartsDeviceVo;
import com.dykj.fireFighting.common.core.service.impl.BaseServiceImpl;
import com.dykj.fireFighting.common.core.utils.UUIDUtils;
import com.dykj.fireFighting.server.socialUnit.firemanger.mapper.KeyPartsMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;


import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 消防管理-重点部位
 *
 * @Author aixiaolan
 * @Date  2021-06-25 14:04:07
 */
@Slf4j
@DubboService
@Transactional(rollbackFor = Exception.class)
public class KeyPartsServiceImpl extends BaseServiceImpl<KeyPartsMapper, KeyParts> implements IKeyPartsService {

    @Resource
    IKeyPartsDeviceService keyPartsDeviceService;
    @Resource
    ICoordinateRelationService coordinateRelationService;
    @Resource
    IPartitionsDeviceService partitionsDeviceService;


    @Transactional
    @Override
    public Boolean insertKeyParts(KeyPartsDeviceVo keyPartsDeviceVo){
        KeyParts keyParts=new KeyParts();
        BeanUtils.copyProperties(keyPartsDeviceVo,keyParts);
        keyParts.setCreateTime(new Date());
        keyParts.setUpdateTime(new Date());
        String tempId= UUIDUtils.get32UUID();
        keyParts.setId(tempId);
        keyParts.setPartitionId(keyPartsDeviceVo.getPartitionId());
        log.info("重点部位写入数据：{}",keyParts.toString());
        save(keyParts);
        for(int i=0;i<keyPartsDeviceVo.getDevice().size();i++){
            KeyPartsDevice keyPartsDevice=new KeyPartsDevice();
            keyPartsDevice.setDeviceId(keyPartsDeviceVo.getDevice().get(i));
            keyPartsDevice.setKeyPartsId(tempId);
            keyPartsDevice.setType(0);
            keyPartsDevice.setId(UUIDUtils.get32UUID());
            keyPartsDeviceService.save( keyPartsDevice);
        }
        for(int i=0;i<keyPartsDeviceVo.getDeviceTradition().size();i++){
            KeyPartsDevice keyPartsDevice=new KeyPartsDevice();
            keyPartsDevice.setDeviceId(keyPartsDeviceVo.getDeviceTradition().get(i));
            keyPartsDevice.setKeyPartsId(tempId);
            keyPartsDevice.setType(1);
            keyPartsDevice.setId(UUIDUtils.get32UUID());
            keyPartsDeviceService.save( keyPartsDevice);
        }
        if(StringUtils.isNotBlank(keyPartsDeviceVo.getPartitionId())){
            PartitionsDevice partitionsDevice=new PartitionsDevice();
            partitionsDevice.setId(UUIDUtils.get32UUID());
            partitionsDevice.setType(101);
            partitionsDevice.setDeviceId(keyParts.getId());
            partitionsDevice.setDeviceName(keyParts.getName());
            partitionsDevice.setY(keyPartsDeviceVo.getLat());
            partitionsDevice.setX(keyPartsDeviceVo.getLng());
            partitionsDevice.setPartitionId(keyPartsDeviceVo.getPartitionId());
            log.info("插入分区坐标关系表：{}",partitionsDevice.toString());
            partitionsDeviceService.insertPoint(partitionsDevice);
        }else{
            //关系表
            CoordinateRelation coordinateRelation=new CoordinateRelation();
            coordinateRelation.setLng(keyPartsDeviceVo.getLng());
            coordinateRelation.setLat(keyPartsDeviceVo.getLat());
            coordinateRelation.setTableId(tempId);
            coordinateRelation.setId(UUIDUtils.get32UUID());
            coordinateRelation.setUnitId(keyPartsDeviceVo.getUnitId());
            coordinateRelation.setTableType("2");
            log.info("插入坐标关系表：{}",coordinateRelation.toString());
            coordinateRelationService.save(coordinateRelation);
        }

        return true;
    }

    /**
     * 正常修改，重要部位中间设备表先删除，再插入
     * @param keyPartsDeviceVo
     * @return
     */
    @Transactional
    @Override
    public Boolean updateKeyParts(KeyPartsDeviceVo keyPartsDeviceVo){
        //修改
        KeyParts keyParts=new KeyParts();
        BeanUtils.copyProperties(keyPartsDeviceVo,keyParts);
        keyParts.setUpdateTime(new Date());
        log.info("重要部位修改数据：{}",keyParts.toString());
        updateById(keyParts);
        //删除中间设备表数据
        keyPartsDeviceService.delByKeyPartsId(keyPartsDeviceVo.getId());
        //写入中间设备表的数据
        String tempId=keyPartsDeviceVo.getId();
        for(int i=0;i<keyPartsDeviceVo.getDevice().size();i++){
            KeyPartsDevice keyPartsDevice=new KeyPartsDevice();
            keyPartsDevice.setDeviceId(keyPartsDeviceVo.getDevice().get(i));
            keyPartsDevice.setKeyPartsId(tempId);
            keyPartsDevice.setType(0);
            keyPartsDevice.setId(UUIDUtils.get32UUID());
            keyPartsDeviceService.save( keyPartsDevice);
        }
        for(int j=0;j<keyPartsDeviceVo.getDeviceTradition().size();j++){
            KeyPartsDevice keyPartsDevice=new KeyPartsDevice();
            keyPartsDevice.setDeviceId(keyPartsDeviceVo.getDeviceTradition().get(j));
            keyPartsDevice.setKeyPartsId(tempId);
            keyPartsDevice.setType(1);
            keyPartsDevice.setId(UUIDUtils.get32UUID());
            keyPartsDeviceService.save( keyPartsDevice);
        }
        //修改坐标关系表
        if(StringUtils.isNotBlank(keyPartsDeviceVo.getPartitionId())){//判断坐标是否存在分区中
            //在分区
            PartitionsDevice partitionsDevice=new PartitionsDevice();
            partitionsDevice.setId(UUIDUtils.get32UUID());
            partitionsDevice.setType(101);
            partitionsDevice.setDeviceId(keyParts.getId());
            partitionsDevice.setDeviceName(keyParts.getName());
            partitionsDevice.setY(keyPartsDeviceVo.getLat());
            partitionsDevice.setX(keyPartsDeviceVo.getLng());
            partitionsDevice.setPartitionId(keyPartsDeviceVo.getPartitionId());
            log.info("插入分区坐标关系表：{}",partitionsDevice.toString());
            int i = partitionsDeviceService.insertPoint(partitionsDevice);
            log.info("分区坐标关系表更新结果：{}",i);
            //删除园区中的坐标
            coordinateRelationService.deleteByTableId(tempId, "2");
        }else{//不在分区
            //判断是否存在坐标
            List<CoordinateRelation> byTableId = coordinateRelationService.getByTableId(tempId, "2");
            if(byTableId.size()>0){//存在
                //根据tableID更新坐标
                boolean result=coordinateRelationService.updateByTableID(keyPartsDeviceVo.getLng(),keyPartsDeviceVo.getLat(),
                        tempId);
                log.info("坐标关系表更新结果：{}",result);
            }else{//不存在插入坐标
                //关系表
                CoordinateRelation coordinateRelation=new CoordinateRelation();
                coordinateRelation.setLng(keyPartsDeviceVo.getLng());
                coordinateRelation.setLat(keyPartsDeviceVo.getLat());
                coordinateRelation.setTableId(tempId);
                coordinateRelation.setId(UUIDUtils.get32UUID());
                coordinateRelation.setUnitId(keyPartsDeviceVo.getUnitId());
                coordinateRelation.setTableType("2");
                log.info("插入坐标关系表：{}",coordinateRelation.toString());
                coordinateRelationService.save(coordinateRelation);
            }
            //删除分区中的坐标
            partitionsDeviceService.deletePoint(keyPartsDeviceVo.getId());
        }

        return true;


    }


}
